package com.vincent.kdp.Activity;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.adapter.ShangjiaAdapter;
import com.vincent.kdp.ui.work.ShangjiaInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ShangjiaActivity extends Activity {

    private ListView shangjia;
    private RequestQueue requestQueue;
    private ShangjiaAdapter adapter;

    public static ShangjiaActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        instance=this;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shangjia);
        initData();
        shangjia=(ListView)findViewById(R.id.shangjia);
        adapter=new ShangjiaAdapter(this);
        shangjia.setAdapter(adapter);
    }

    private void initData() {
        requestQueue= Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, PostUrl.SHANGJIA, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("code",s);
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(s);
                    System.out.println("jsonObject--SHANGJIA--"+jsonObject);
                    if(jsonObject.opt("msg").equals("系统错误")){
                        Toast.makeText(ShangjiaActivity.this,jsonObject.opt("msg")+",请重试",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    JSONArray array = (JSONArray) jsonObject.get("data");
                    System.out.println("jsonObject--SHANGJIA--data--"+array);
                    List<ShangjiaInfo> list = new ArrayList<ShangjiaInfo>();
                    for (int i = 0; i < array.length(); i++) {
                        ShangjiaInfo info=new ShangjiaInfo();
                        JSONObject temp = (JSONObject) array.get(i);
                         String id ="";
                        String name = "";
                        if (temp.has("id")) {
                            id = String.valueOf(temp.getInt("id"));
                        }
                        if (temp.has("name")) {
                            name = temp.getString("name");
                        }
                        System.out.println("jsonObject--SHANGJIA--data--name--"+name);
                        info.setId(Integer.parseInt(id));
                        info.setXinxi(name);
                        list.add(info);
                    }
                    adapter.addItems(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                return map;
            }


        };
        requestQueue.add(stringRequest);
    };


}
